//
// Created by Fish on 24-12-27.
//
#pragma once
#include<iostream>

#include"node/Node.hpp"

template<typename T>
class List {
  Node<T> *head;
  Node<T> *tail;
  int size;
public:
  List() : head(nullptr), tail(nullptr), size(0) {}
  void add(T data) {
    auto *node = new Node<T>(data);
    if (head == nullptr) {
      head = node;
    } else {
      tail->next = node;
    }
    tail = node;
    size++;
  }

  void print() {
    Node<T> *current = head;
    while (current != nullptr) {
      std::cout << current->data << " ";
      current = current->next;
    }
    std::cout << std::endl;
  }

  Node<T>* search(T data) {
    Node<T> *current = head;
    while (current != nullptr) {
      if (current->data == data) {
        return current;
      }
      current = current->next;
    }
    return nullptr;
  }

  void add_back(T data, T data2)
  {
    auto node = search(data);
    if (node != nullptr) {
      auto *node2 = new Node<T>(data2);
      node2->next = node->next;
      node->next = node2;
    } else {
      std::cout << "Node not found" << std::endl;
    }
  }

};

namespace L_name
{
  inline void test()
  {
    List<int> list;
    list.add(1);
    list.add(2);
    list.add(3);
    list.add_back(2, 4);
    list.print();

    List<char> list2;
    list2.add('a');
    list2.add('b');
    list2.add('c');
    list2.add_back('a', 'd');
    list2.print();
  }
}
